/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.uct.service.plugins  
 */
package com.apache.member.service.plugins;

import org.apache.log4j.Logger;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.member.common.ConstantData;
import com.apache.member.entity.Member;
import com.apache.member.manager.MemberManager;
import com.apache.tools.ClassToolsUtil;
import com.apache.uct.common.entity.User;

/**
 * description:  会员新增插件
 * @author dwusoft 创建时间：2016-7-28  
 * @Copyright(c)2014:北京--软件技术有限公司
 */
public class MemberSavePlugin implements PluginConnector {

	private Logger log = Logger.getLogger(MemberSavePlugin.class);

	private MemberManager memberManager;

	/**
	 * TODO 用户新增插件.  <br>
	 * 参数说明:<br>
	 * exeType:执行方法标识(save/edit)<br>
	 * user对象属性;userEname不可空<br>
	 * actFullEname:需要给用户赋的权限;<br>
	 * roleId:需要给用户赋的角色ID
	 */
	public Object execute(ParamsVo vo) throws Exception {
		ResultEntity result = new ResultEntity();
		try {
			result.setResult("true");
			String exeType = String.valueOf(vo.getParams("doCode"));
			String memberId = String.valueOf(vo.getParams("memberId"));
			if ("memberSave".equals(exeType) && Validator.isNull(memberId)) {
				addMemberAndRoles(result, vo);
			} else if ("memberSave".equals(exeType) && Validator.isNotNull(memberId)) {
				editMember(result, vo);
			} else {
				result.setMessage("没有可用方法被执行！");
			}
		} catch (Exception e) {
			result.setResult("false");
			result.setMessage("执行失败！");
		}
		return result;
	}

	/**
	 * description:  新增会员，同时默认给他加一条memberRole信息。
	 */
	private void addMemberAndRoles(ResultEntity result, ParamsVo vo) {
		Member member = new Member();
		member = (Member) ClassToolsUtil.getInstance().convert2Bean(vo.getParams(), member);

		if (member != null) {
			ParamsVo<Member> memberVo = new ParamsVo<Member>();
			memberVo.setObj(member);
			Long num = checkMemberEname(memberVo);//判断该会员名是否存在
			if (num == 0) {
				member.setAuditStatus(ConstantData.AuditStatus.AUDITING);
				memberVo.setObj(member);
				memberVo.setParams("userId", vo.getParams("userId"));
				String memberId = memberManager.saveMemberAndRoles(memberVo);
				if (Validator.isNull(memberId)) {
					result.setMessage("会员新增失败");
				} else {
					result.setEntity(memberId);
					result.setMessage("会员新增成功");
				}
			} else {
				result.setMessage("已存在该会员名！");

			}
		}
	}

	/**
	 * description:  修改会员信息。
	 * 如果此时
	 */
	private void editMember(ResultEntity result, ParamsVo vo) {

		Member member = new Member();
		member = (Member) ClassToolsUtil.getInstance().convert2Bean(vo.getParams(), member);

		if (member != null) {
			ParamsVo<Member> memberVo = new ParamsVo<Member>();
			if (Validator.isNotNull(member.getMemberId())) {
				memberVo.setObj(member);
				boolean flag = memberManager.editInfo(memberVo);
				if (!flag) {
					result.setMessage("会员修改失败");
				} else {
					result.setMessage("会员修改成功");
					result.setEntity(member.getMemberId());
				}
			} else {
				result.setMessage("修改用户不存在：用户唯一标识未传递或传递的唯一标识不存在");

			}
		}
	}

	private Long checkMemberEname(ParamsVo<Member> memberVo) {
		memberVo.setKey("checkMemberEname");
		Member memberObj = new Member();
		memberObj.setMemberEname(memberVo.getObj().getMemberEname());
		memberVo.setObj(memberObj);
		Long num = (Long) memberManager.execute(memberVo);//验证用户是否存在
		return num;
	}

	//数据验证
	private String checkUserInfo(String checkColumn, User user) {
		if (!"1".equals(checkColumn)) {
			if (checkColumn.contains("email")) {//验证邮箱必填
				if (Validator.isNull(user.getEmail())) {
					return "邮箱不能为空";
				}
			}
			if (checkColumn.contains("mobile")) {//验证手机号必填
				if (Validator.isNull(user.getMobile())) {
					return "手机号不能为空";
				}
			}
			if (checkColumn.contains("orgEname")) {//机构标识
				if (Validator.isNull(user.getOrgEname())) {
					return "机构标识不能为空";
				}
			}
		}

		return "";
	}

	private String getParamStr(ParamsVo vo, String attrName) {
		return Validator.getDefaultStr(String.valueOf(vo.getParams(attrName)), "");
	}

	public void setMemberManager(MemberManager memberManager) {
		this.memberManager = memberManager;
	}

}
